package com.xuln.junit.framework.struts2;

import javax.annotation.Resource;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;
import com.xuln.junit.framework.hibernate.TestEntity;

public class LoginAction extends ActionSupport {
	private static final long serialVersionUID = -6934061755046941680L;
	private static final Logger logger = Logger.getLogger(LoginAction.class);

	private String username;
	private String password;
	@Resource
	private LoginService loginService;

	private boolean isInvalid(String value) {
		return ((value == null) || (value.length() == 0));
	}

	public String login() throws Exception {
		if (isInvalid(getUsername()))
			return "input";

		if (isInvalid(getPassword()))
			return "input";

		boolean isLogin = loginService.login(username, password);
		if (isLogin) {
			logger.info(username + "登录成功");
		}
		return "success";
	}

	public String getUserInfo(Long userId) {
		TestEntity user = loginService.getUserInfo(userId);
		logger.info("UserInfo:username=" + user.getName());
		return "success";
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}